/*
 * ClienteDAO.java
 *
 * Created on 8 de marzo de 2007, 03:11 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.src.computec.radar.services.utilities.dao;

import com.src.computec.radar.base.dao.GenericDAO;
import com.src.computec.radar.base.dao.exception.DAOException;
import com.src.computec.radar.services.base.constantes.ConstantesGenerales;
import com.src.computec.radar.services.utilities.dto.ClienteDto;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author aocampo
 */
public class ClienteDAO extends GenericDAO implements BaseClienteDAO {
    
    /** Creates a new instance of ClienteDAO */
    public ClienteDAO() {
    }
    
    public List getClientes() throws DAOException {
        List listClientes = new ArrayList();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = getConnectionDB2();
            statement = con.prepareStatement("SELECT CODIGO_CLIENTE, RAZON_SOCIAL " +
                                             "FROM " + ConstantesGenerales.getInstance().SCHEMA + ".CLIENTE " +
                                             "ORDER BY RAZON_SOCIAL ASC");
            rs = statement.executeQuery();
            while (rs.next()) {
                int indice = 0;
                ClienteDto clienteDto = new ClienteDto();
                clienteDto.setCodigoCliente(rs.getString(++indice));
                clienteDto.setRazonSocial(rs.getString(++indice));
                listClientes.add(clienteDto);
            }
        } 
        catch (SQLException e) {
            throw new DAOException("ClienteDAO:getClientes: " + e.toString(), e);
        }
        finally {
            closeConnection(con, statement, rs);
        }
        return listClientes;
    }
    
    public ClienteDto getClienteByPrimaryKey(ClienteDto dto) throws DAOException 
    {
        ClienteDto clienteDto = null;
        
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = getConnectionDB2();
            statement = con.prepareStatement("SELECT CODIGO_CLIENTE, RAZON_SOCIAL " +
                                             "FROM " + ConstantesGenerales.getInstance().SCHEMA + ".CLIENTE " +
                                             "WHERE  CODIGO_CLIENTE = ? ");
            int indice = 0;
            statement.setObject(++indice,dto.getCodigoCliente());
            rs = statement.executeQuery();
            if (rs.next()) 
            {
                indice = 0;
                clienteDto = new ClienteDto();
                clienteDto.setCodigoCliente(rs.getString(++indice));
                clienteDto.setRazonSocial(rs.getString(++indice));                
            }
        } 
        catch (SQLException e) {
            throw new DAOException("ClienteDAO:getClienteByPrimaryKey: " + e.toString(), e);
        }
        finally {
            closeConnection(con, statement, rs);
        }
        return clienteDto;
    }
}